import os
import requests
from urllib.parse import urlparse
import oss2 as oss

# 登录信息
# USERNAME = "17141177900"
# TARGET_URL = "https://www.geekbi.com"
# USERNAME = "13266326150"
# TARGET_URL = "https://seerfar.cn/admin/sign-in.html"
# 特看数据
USERNAME = "18058204033"
TARGET_URL = "https://www.tabcut.com/zh-CN/workbench"

# 萌啦数据
# USERNAME = "13266326150"
# TARGET_URL = "https://ozon.menglar.com/workbench/dashboard"



# 特看数据
# https://www.tabcut.com/zh-CN/workbench
# 账号 18058204033
# 密码 86713626a

# 萌啦数据
# https://ozon.menglar.com/workbench/dashboard
# 账号 13266326150
# 密码 86713626a

# fastmoss
# https://www.fastmoss.com/zh
# 账号 13266326150
# 密码 86713626a

PASSWORD = ""

# 目标网址


# 登录方式
LOGIN_TYPE = "手机号"


LOGIN_PATH = ""
MAIN_PATH = ""

AUTH_FILE_NAME = USERNAME+"_auth_state.json"
JSON_PATH = "https://auth-share.oss-cn-shenzhen.aliyuncs.com/"+AUTH_FILE_NAME



def get_storage_state_path():
    if JSON_PATH.startswith("http"):
        # 下载文件并保存为auth_state.json
        print("下载文件并保存为auth_state.json")
        response = requests.get(JSON_PATH)
        response.raise_for_status()
        
        filename = AUTH_FILE_NAME
        with open(filename, "wb") as f:
            f.write(response.content)
            print(f"文件保存成功: {filename}")
        return filename
    return JSON_PATH

def upload_file_to_oss(file_path):
    region = "cn-shenzhen"
    bucket_name = "auth-share"
    object_name = file_path
    local_file  = file_path
    os.environ["OSS_ACCESS_KEY_ID"] = "LTAI5t6eT7CRkj6hTQrTVXxE"
    os.environ["OSS_ACCESS_KEY_SECRET"] = "JL9FWMfdg42QR2HKBIhPLIE43yBmr3"

    endpoint = 'oss-cn-shenzhen.aliyuncs.com' # 假设你的Bucket处于杭州区域

    auth = oss.Auth(os.environ["OSS_ACCESS_KEY_ID"], os.environ["OSS_ACCESS_KEY_SECRET"])
    bucket = oss.Bucket(auth, endpoint, bucket_name)


    with open(local_file, 'rb') as f:
        result = bucket.put_object(key=object_name, data=f)
        print(result.status)

    print(f'put object successfully, ETag {result.etag}')


